﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmProductInCheck : Form
    {
        private DataTable DtIn;
        private DataTable DtBoxInfo;
        private List<string> checkedBoxNoList = new List<string>();

        public FrmProductInCheck()
        {
            InitializeComponent();
        }

        private void FrmProductInCheck_Load(object sender, EventArgs e)
        {
            BindCBX();
        }

        /// <summary>
        /// 入库单据号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbx_Doc_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        /// <summary>
        /// 入库单据号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbx_Doc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

        /// <summary>
        /// 箱号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_BoxNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (!this.btn_confirm.Enabled)
            {
                MessageBox.Show("当前入库单已验收完成!");
                return;
            }

            if (string.IsNullOrEmpty(cbx_Doc.Text))
            {
                MessageBox.Show("请选择单号!");
                cbx_Doc.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txt_BoxNo.Text))
            {
                MessageBox.Show("箱号不能为空!");
                txt_BoxNo.Focus();
                return;
            }

            var splitList = txt_BoxNo.Text.Split('-');
            var boxNo = splitList.FirstOrDefault();
            txt_BoxNo.Text = boxNo;

            // 判断箱号是否存在与当前入库单中
            DataRow[] boxInfo = DtBoxInfo.Select(string.Format(" BoxNo = '{0}'", boxNo));
            if (boxInfo == null || boxInfo.Length == 0)
            {
                MessageBox.Show("该箱号:" + boxNo + "不属于当前入库单!");
                txt_BoxNo.Text = "";
                txt_BoxNo.Focus();
                return;
            }

            if (checkedBoxNoList.Contains(boxNo))
            {
                MessageBox.Show("该箱号:" + boxNo + "已验收!");
                txt_BoxNo.Text = "";
                txt_BoxNo.Focus();
                return;
            }

            checkedBoxNoList.Add(txt_BoxNo.Text);
            lab_BoxNum.Text = string.Format("{0}/{1}", checkedBoxNoList.Count, DtBoxInfo.Rows.Count);
            appendLog(string.Format("箱号:{0}", boxNo));
            txt_BoxNo.Text = "";
            txt_BoxNo.Focus();

            // 已验证件数与总件数一致时，结束
            if (checkedBoxNoList.Count == DtBoxInfo.Rows.Count)
            {
                btn_confirm.Enabled = false;
                appendLog("验收完成。");
            }
        }

        /// <summary>
        /// 完成按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_confirm_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否强制结束验收?", "验收结束确认", MessageBoxButtons.OKCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
            if (dr != DialogResult.OK)
            {
                return;
            }
            btn_confirm.Enabled = false;
            appendLog("验收完成。");
        }

        /// <summary>
        /// 初始化如单据号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BindCBX()
        {
            DtIn = Bll.Bll_ProductIn.GetInStockProductForCheck();
            cbx_Doc.Text = "";
            cbx_Doc.Items.Clear();

            if (DtIn.Rows.Count > 0)
            {
                foreach (DataRow dr in DtIn.Rows)
                {
                    cbx_Doc.Items.Add(dr["BillNo"].ToString());
                }
            }
        }

        /// <summary>
        /// 选择入库单据号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InputBillNo()
        {
            DataRow[] drIn = DtIn.Select(string.Format(" BillNo = '{0}'", cbx_Doc.Text.ToString()));
            if (drIn.Length > 0)
            {
                // 检索入库单下所有箱信息
                DtBoxInfo = Bll.Bll_ProductIn.GetBoxNoList(cbx_Doc.Text.ToString());
                txt_BoxNo.Text = "";
                txt_BoxNo.Focus();
                checkedBoxNoList.Clear();
                lab_BoxNum.Text = string.Format("{0}/{1}", checkedBoxNoList.Count, DtBoxInfo.Rows.Count);
                lab_msg.Text = "";
                appendLog("当前单号:" + cbx_Doc.Text);
                btn_confirm.Enabled = true;
            }
            else
            {
                MessageBox.Show(cbx_Doc.Text + "单号不正确,请输入正确的单据!");
                lab_BoxNum.Text = "";
                cbx_Doc.Focus();
                cbx_Doc.Text = "";
                txt_BoxNo.Text = "";
                lab_BoxNum.Text = "";
                lab_msg.Text = "";
                checkedBoxNoList.Clear();
                btn_confirm.Enabled = false;
            }
        }

        /// <summary>
        /// 显示LOG
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void appendLog(string log)
        {
            if (lab_msg.Text.Length > 0)
            {
                log = "\r\n" + log;
            }
            lab_msg.Text += log;
            lab_msg.Select(lab_msg.TextLength, 0);
            lab_msg.ScrollToCaret();
        }
    }
}